To open the SharePoint 2010 Management Shell, click
Start, and then select Programs, Microsoft SharePoint 2010 Products,
and finally, SharePoint 2010 Management Shell, as shown in Figure 1.
The SharePoint 2010
Management Shell is a custom console; it is not the same console that
opens if you open the default Windows PowerShell console, a shortcut to
which is usually placed on the taskbar. A review of the properties of
the SharePoint 2010 Management Shell shortcut exposes the command that
is executed.
C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe -NoExit " &
' C:\Program Files\Common Files\Microsoft Shared\Web Server
Extensions\14\CONFIG\POWERSHELL\Registration\\sharepoint.ps1 ' "
The shortcut points to the
file Sharepoint.ps1. This is an example of a Windows PowerShell
profile, which is identical to any other Windows PowerShell script file
and is used to store frequently used elements that need to be
automatically loaded at the start of a Windows PowerShell session.
Hence, just like normal scripting files, profiles can contain
functions, aliases, and variables, and they can load any Windows
PowerShell extensions, snap-ins, or modules you might need. A Windows
PowerShell snap-in (PSSnapin) is a .NET program that is compiled into
DLL files and contains new cmdlets, functions, and/or providers.
Windows PowerShell comes with a number of snap-ins, including
Microsoft.PowerShell.Core, Microsoft.PowerShell.Host, and
Microsoft.PowerShell.WSMan.Management, as well as modules such as
ActiveDirectory, FailoverClusters, and WebAdministration. As other
products are installed on your computer, additional extensions will
become available.
The main aim of the
SharePoint profile file is to load the Windows PowerShell for
SharePoint snap-in so you can then use the new cmdlets specific to
SharePoint. The SharePoint profile file contains the following code,
plus a signature.
$ver = $host | select version
if ($ver.Version.Major -gt 1) {$Host.Runspace.ThreadOptions = "ReuseThread"}
Add-PsSnapin Microsoft.SharePoint.PowerShell
Set-location $home
This code obtains the version
of Windows PowerShell, checks that it is greater than 1, and then if
that is true, it sets the threading model so that the first thread will
be reused, loads the SharePoint PowerShell snap-in, and then changes
directory to the home folder, such as C:\users\<userid>, where userid
is the person who is currently logged in.
The SharePoint PowerShell
snap-in is not the only snap-in loaded into the SharePoint 2010
Management Shell. Therefore, not only can you use the SharePoint
cmdlets, you also can use other cmdlets from the snap-ins that are
loaded. To see all the snap-ins and the order in which they were
loaded, type Get-PSSnapin. Your output should look similar to the following example.
Name : Microsoft.PowerShell.Diagnostics
PSVersion : 2.0
Description : This Windows PowerShell snap-in contains Windows Eventing and
Performance Counter cmdlets.
Name : Microsoft.WSMan.Management
PSVersion : 2.0
Description : This Windows PowerShell snap-in contains cmdlets (such as
Get-WSManInstance and Set-WSManInstance) that are used by the
Windows PowerShell host to manage WSMan operations.
Name : Microsoft.PowerShell.Core
PSVersion : 2.0
Description : This Windows PowerShell snap-in contains cmdlets used to manage
components of WindowsPowerShell.
Name : Microsoft.PowerShell.Utility
PSVersion : 2.0
Description : This Windows PowerShell snap-in contains utility Cmdlets used to
manipulate data.
Name : Microsoft.PowerShell.Host
PSVersion : 2.0
Description : This Windows PowerShell snap-in contains cmdlets (such as Start-
Transcript and Stop-Transcript) that are provided for use with the
Windows PowerShell console host.
Name : Microsoft.PowerShell.Management
PSVersion : 2.0
Description : This Windows PowerShell snap-in contains management cmdlets used to
manage Windows components.
Name : Microsoft.PowerShell.Security
PSVersion : 2.0
Description : This Windows PowerShell snap-in contains cmdlets to manage Windows
PowerShell security.
Name : Microsoft.SharePoint.PowerShell
PSVersion : 1.0
Description : Register all administration Cmdlets for Microsoft SharePoint Server.
Note:
If you develop your own scripts
that can be called from the command prompt or by double-clicking the
file in Windows Explorer, and those scripts contain Windows PowerShell
for SharePoint commands, make sure that you include the command to set
the threading model and load the Windows PowerShell for SharePoint
snap-in in your scripts. When calling a script from within a Windows
PowerShell console, you can use the #requires tag at the top of your
script.
It is common practice to
modify the profile file on the designated administrator machine. The
profile could be altered so that it
Contains all the snap-ins or modules used by all IT professionals in an organization or department.
Tracks all Windows PowerShell commands executed.
Changes
the prompt to indicate the server name. In an organization with a
multitude of servers, it is all too easy to start an administrative
task on the wrong server, so anything that can help an administrator
identify the server he is working on will be helpful, such as
configuring the window title to list the name of the server.
An example of a custom profile could contain the following code.
<# ***********************************************************
Copyright (c)2010, Contoso, All Rights Reserved
*************************************************************
Contoso PowerShell Profile - ContosoProfile.ps1
Author: Peter Connelly
Purpose: This profile sets the PowerShell window size, font and title
and loads the SharePoint snap-in
History: Version 1.0 01/02/2010 First version
#>
# Track all Windows PowerShell commands
$profilename = $MyInvocation.MyCommand.Name
$profilepath = $MyInvocation.MyCommand.Path
$transcriptFile = "C:\Contoso\Logs\Powershell_$profilename.log"
Start-Transcript $transcriptFile -append -force
Write-Output "Starting profile: $profilepath"
# Command from SharePoint Server 2010 profile file - SharePoint.ps1
$ver = $host | select version
if ($ver.Version.Major -gt 1) {$Host.Runspace.ThreadOptions = "ReuseThread"}
Add-PsSnapin Microsoft.SharePoint.PowerShell
Set-location $home
# Check that this is a command-line interface and not the ISE
if ($host.name -eq "ConsoleHost")
{
$width = 80
$sizeWindow = new-object System.Management.Automation.Host.Size $width,40
$sizeBuffer = new-object System.Management.Automation.Host.Size $width,9999
<# Check to adhere to the following rules:
The buffer width can't be resized to be narrower than the window's current
width plus the window's width can't be resized to be wider than the
buffer's current width. #>
$S = $Host.UI.RawUI
if ($s.WindowSize.width -gt $width)
{
$s.WindowSize = $sizeWindow
$s.BufferSize = $sizeBuffer
} else {
$s.BufferSize = $sizeBuffer
$s.WindowSize = $sizeWindow
}
}
# Set foreground, background color and window title
$s.ForegroundColor = "Yellow";$s.BackgroundColor = "DarkBlue";
$s.WindowTitle = "$env:computername"
# #############################################################
# End of ContosoProfile.ps1
# #############################################################
Because the profile
script is automatically executed every time you run the SharePoint 2010
Management Shell, it should be signed. More information about
customizing your console can be found at http://technet.microsoft.com/en-us/library/ee156814.aspx.